/*
 * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
 *
 * Copyright (C) 2019 Tencent.  All rights reserved.
 *
 * BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
 *
 * A copy of the MIT License is included in this file.
 *
 *
 * Terms of the MIT License:
 * ---------------------------------------------------
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of
 * the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.tencent.bkrepo.replication.dao

import com.tencent.bkrepo.common.mongo.dao.simple.SimpleMongoDao
import com.tencent.bkrepo.replication.model.TFederatedRepository
import com.tencent.bkrepo.replication.pojo.federation.FederatedCluster
import org.springframework.data.mongodb.core.query.Criteria
import org.springframework.data.mongodb.core.query.Query
import org.springframework.data.mongodb.core.query.Update
import org.springframework.stereotype.Repository
import java.time.LocalDateTime

@Repository
class FederatedRepositoryDao : SimpleMongoDao<TFederatedRepository>() {

    fun findByProjectIdAndRepoName(
        projectId: String, repoName: String, federationId: String? = null
    ): List<TFederatedRepository> {
        val criteria = Criteria.where(TFederatedRepository::projectId.name).`is`(projectId)
            .and(TFederatedRepository::repoName.name).`is`(repoName)
        federationId?.let { criteria.and(TFederatedRepository::federationId.name).`is`(it) }
        return find(Query(criteria))
    }

    /**
     * 根据名称查询联邦仓库
     */
    fun findByName(name: String): List<TFederatedRepository> {
        val criteria = Criteria.where(TFederatedRepository::name.name).`is`(name)
        return find(Query(criteria))
    }

    fun deleteByProjectIdAndRepoName(projectId: String, repoName: String, federationId: String) {
        val criteria = Criteria.where(TFederatedRepository::projectId.name).`is`(projectId)
            .and(TFederatedRepository::repoName.name).`is`(repoName)
            .and(TFederatedRepository::federationId.name).`is`(federationId)
        val query = Query(criteria)
        remove(query)
    }

    /**
     * 更新全量同步状态为开始
     */
    fun updateFullSyncStart(projectId: String, repoName: String, federationId: String): Boolean {
        val criteria = Criteria.where(TFederatedRepository::projectId.name).`is`(projectId)
            .and(TFederatedRepository::repoName.name).`is`(repoName)
            .and(TFederatedRepository::federationId.name).`is`(federationId)
            .and(TFederatedRepository::isFullSyncing.name).ne(true) // 兼容历史数据：null或false都可以开始同步

        val update = Update()
            .set(TFederatedRepository::isFullSyncing.name, true)
            .set(TFederatedRepository::lastFullSyncStartTime.name, LocalDateTime.now())
            .set(TFederatedRepository::lastModifiedDate.name, LocalDateTime.now())

        val result = updateFirst(Query(criteria), update)
        return result.modifiedCount > 0
    }

    /**
     * 更新全量同步状态为完成
     */
    fun updateFullSyncEnd(projectId: String, repoName: String, federationId: String) {
        val criteria = Criteria.where(TFederatedRepository::projectId.name).`is`(projectId)
            .and(TFederatedRepository::repoName.name).`is`(repoName)
            .and(TFederatedRepository::federationId.name).`is`(federationId)

        val update = Update()
            .set(TFederatedRepository::isFullSyncing.name, false)
            .set(TFederatedRepository::lastFullSyncEndTime.name, LocalDateTime.now())
            .set(TFederatedRepository::lastModifiedDate.name, LocalDateTime.now())

        updateFirst(Query(criteria), update)
    }

    /**
     * 检查是否正在进行全量同步
     */
    fun isFullSyncing(projectId: String, repoName: String, federationId: String): Boolean {
        val criteria = Criteria.where(TFederatedRepository::projectId.name).`is`(projectId)
            .and(TFederatedRepository::repoName.name).`is`(repoName)
            .and(TFederatedRepository::federationId.name).`is`(federationId)
            .and(TFederatedRepository::isFullSyncing.name).`is`(true)

        return exists(Query(criteria))
    }

    /**
     * 更新联邦仓库的集群列表
     */
    fun updateFederatedClusters(
        projectId: String,
        repoName: String,
        federationId: String,
        updatedClusters: List<FederatedCluster>
    ) {
        val criteria = Criteria.where(TFederatedRepository::projectId.name).`is`(projectId)
            .and(TFederatedRepository::repoName.name).`is`(repoName)
            .and(TFederatedRepository::federationId.name).`is`(federationId)

        val update = Update()
            .set(TFederatedRepository::federatedClusters.name, updatedClusters)
            .set(TFederatedRepository::lastModifiedDate.name, LocalDateTime.now())

        updateFirst(Query(criteria), update)
    }
}